import {
  getAccountList,
  editAccountList
} from '@/api/system'

const state = {
  accountData: [],
  accountDataCount: 0
}

const mutations = {
  GET_ACCOUNT_DATA: (state, data) => {
    state.accountData = data
  },
  GET_ACCOUNT_DATA_COUNT: (state, data) => {
    state.accountDataCount = data
  }
}

const actions = {
  async getAccountData({ commit }, playload) {
    let data = {}
    let newData = []
    if (Object.keys(playload).length > 0) {
      data = playload
    }
    const resp = await getAccountList(data)
    const host = resp.data.host
    resp.data.data.map((item) => {
      newData = [...newData, {
        id: item.id,
        host: host,
        tel: item.tel,
        token: item.token,
        state: item.state,
        uname: item.uname,
        password: item.password,
        cpassword: '',
        role: item.role,
        ctime: item.ctime,
        img: item.img,
        pid: item.pid,
        gname: item.gname,
        pname: item.pname,
        openState: item.state === 0
      }]
    })
    // for (let i = 0; i < 5; i++) {
    //   newData = [...newData, {
    //     id: i + 1,
    //     ctime: '2019-04-24 14:58:00',
    //     name: '张三',
    //     number: '15961100051',
    //     role: '公司业务员',
    //     address: '江苏常州',
    //     updatetime: '2019-04-24 16:58:00',
    //     state: 0
    //   }]
    // }
    commit('GET_ACCOUNT_DATA', newData)
    // commit('GET_ACCOUNT_DATA_COUNT', resp.data.total)
    return resp
  },
  async editAccountData({ commit }, playload) {
    const resp = await editAccountList(playload)
    return resp
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
